import 'package:flutter/material.dart';
import 'package:flutter_baselib/lib.dart';

import 'config/res_config_impl.dart';
import 'module/login/view/login_view.dart';
import 'net/config/http_config_impl.dart';

///@date:  2021/3/1 15:20
///@author:  lixu
///@description:
class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  String _appInfo;
  String _deviceInfo;

  @override
  void initState() {
    super.initState();

    initBaseLib();
  }

  ///初始化基础库
  void initBaseLib() async {
    await BaseLibPlugin.init(
      isDebug: true,
      logConfig: LogConfig.obtain(
        tag: "lixu",
        isConsolePrintLog: true,
        isFlutterPrintLog: false,
        saveLogFilePath: '/storage/emulated/0/flutter_xlog_app/xlog/',
        isLogFileEncrypt: false,
      ),
      httpConfig: HttpConfigImpl(),
      resConfig: ResConfigImpl(),
      buglyConfig: BuglyConfig.defaultAndroid('8b841196c7'),
      toastConfig: ToastConfig.obtain(
        fontSize: 14,
        gravity: ToastGravity.BOTTOM,
        backgroundColor: Colors.black87,
        textColor: Colors.white,
      ),
    );

    _appInfo = '应用名称：${await PackageUtils.getAppName()}\n'
        '应用包名：${await PackageUtils.getPackageName()}\n'
        'VersionName：${await PackageUtils.getVersionName()}\n'
        'VersionCode：${await PackageUtils.getBuildNumber()}\n';

    _deviceInfo = '品牌：${await DeviceUtils.getBrand()}\n'
        '型号：${await DeviceUtils.getModel()}\n'
        '版本：${await DeviceUtils.getOsVersion()}\n';
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('首页'),
      ),
      body: Container(
        child: SingleChildScrollView(
          child: Column(
            children: [
              Text('$_appInfo'),
              Text('$_deviceInfo'),
              RaisedButton(
                child: Text('获取存储权限'),
                onPressed: () async {
                  bool isDefine = await PermissionUtils.requestPermission(Permission.storage);
                  if (isDefine) {
                    ToastUtils.show('已获取存储权限');
                  } else {
                    ToastUtils.show('存储权限未开启');
                  }
                },
              ),
              RaisedButton(
                child: Text('检测更新应用'),
                onPressed: () {
                  FlutterBugly.checkUpgrade();
                },
              ),
              RaisedButton(
                child: Text('进入登录页面'),
                onPressed: () {
                  return Navigator.push(context, MaterialPageRoute(builder: (context) {
                    return LoginView();
                  }));
                },
              ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    BaseLibPlugin.dispose();
  }
}
